package com.yf.exam.modules.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.ability.log.annon.LogInject;
import com.yf.ability.log.enums.LogType;
import com.yf.base.api.annon.DataProtect;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.BaseIdsReqDTO;
import com.yf.base.api.api.dto.BaseStateReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.BeanMapper;
import com.yf.exam.modules.exam.dto.CreateAiExamDto;
import com.yf.exam.modules.exam.dto.ExamDTO;
import com.yf.exam.modules.exam.dto.OpenAIExamDTO;
import com.yf.exam.modules.exam.dto.request.ExamNotifyReqDTO;
import com.yf.exam.modules.exam.dto.request.ExamReqDTO;
import com.yf.exam.modules.exam.dto.request.ExamSaveReqDTO;
import com.yf.exam.modules.exam.dto.response.ExamCheckRespDTO;
import com.yf.exam.modules.exam.dto.response.ExamOnlineRespDTO;
import com.yf.exam.modules.exam.dto.response.ExamReviewRespDTO;
import com.yf.exam.modules.exam.entity.Exam;
import com.yf.exam.modules.exam.service.ExamService;
import com.yf.exam.modules.exam.service.OpenCreateAiExamService;
import com.yf.system.modules.user.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api(tags={"考试管理"})
@RestController
@RequestMapping("/api/openAI/exam/exam")
public class OpenAIExamController extends BaseController {
    @Autowired
    private ExamService baseService;

    @Autowired
    private OpenCreateAiExamService openCreateAiExamService;


    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @LogInject(title="添加或修改考试", logType = LogType.EXAM)
    @DataProtect(clazz = Exam.class, update = true)
    @ApiOperation(value = "添加或修改")
    @PostMapping("/save")
    public ApiRest save(@RequestBody ExamSaveReqDTO reqDTO) {
        //复制参数
        baseService.save(reqDTO);
        return super.success();
    }


    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @LogInject(title="添加或修改考试", logType = LogType.EXAM)
    @DataProtect(clazz = Exam.class, update = true)
    @ApiOperation(value = "添加或修改")
    @PostMapping("/saveById")
    public ApiRest saveById(@RequestBody ExamSaveReqDTO reqDTO) {
        //复制参数
        String s = baseService.saveById(reqDTO);
        return super.success(s);
    }

    /**
     * 批量删除
     * @param reqDTO
     * @return
     */
    @LogInject(title="批量删除考试", logType = LogType.EXAM)
    @DataProtect(clazz = Exam.class, delete = true)
    @ApiOperation(value = "批量删除")
    @PostMapping(value = "/delete")
    public ApiRest delete(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.delete(reqDTO.getIds());
        return super.success();
    }

    /**
     * 查找详情
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情")
    @PostMapping("/detail")
    public ApiRest<ExamSaveReqDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        ExamSaveReqDTO dto = baseService.findDetail(reqDTO.getId());
        return super.success(dto);
    }

    /**
     * 单表简略详情
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "简略详情")
    @PostMapping("/simple-detail")
    public ApiRest<ExamDTO> simpleDetail(@RequestBody BaseIdReqDTO reqDTO) {
        Exam exam = baseService.getById(reqDTO.getId());
        ExamDTO respDTO = new ExamDTO();
        BeanMapper.copy(exam, respDTO);
        return super.success(respDTO);
    }

    /**
     * 修改状态
     * @param reqDTO
     * @return
     */
    @LogInject(title="更改考试状态", logType = LogType.EXAM)
    @DataProtect(clazz = Exam.class, update = true)
    @ApiOperation(value = "修改状态")
    @PostMapping("/state")
    public ApiRest state(@RequestBody BaseStateReqDTO reqDTO) {

        QueryWrapper<Exam> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Exam::getId, reqDTO.getIds());
        Exam exam = new Exam();
        exam.setState(reqDTO.getState());
        exam.setUpdateTime(new Date());

        baseService.update(exam, wrapper);
        return super.success();
    }


    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "在线考试列表-学员端")
    @PostMapping("/online-paging")
    public ApiRest<IPage<ExamOnlineRespDTO>> myPaging(@RequestBody PagingReqDTO<ExamReqDTO> reqDTO) {

        //分页查询并转换
        IPage<ExamOnlineRespDTO> page = baseService.onlinePaging(reqDTO);
        return super.success(page);
    }

    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public ApiRest<IPage<ExamDTO>> paging(@RequestBody PagingReqDTO<ExamReqDTO> reqDTO) {

        //分页查询并转换
        IPage<ExamDTO> page = baseService.paging(reqDTO);
        return super.success(page);
    }


    /**
     * 待阅试卷列表
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "待阅试卷列表")
    @PostMapping("/review-paging")
    public ApiRest<IPage<ExamReviewRespDTO>> reviewPaging(@RequestBody PagingReqDTO<ExamReqDTO> reqDTO) {
        //分页查询并转换
        IPage<ExamReviewRespDTO> page = baseService.reviewPaging(reqDTO);
        return super.success(page);
    }


    @ApiOperation(value = "校验考试信息-学员端")
    @PostMapping("/check-info")
    public ApiRest<ExamCheckRespDTO> checkInfo(@RequestBody BaseIdReqDTO reqDTO) {
        ExamCheckRespDTO respDTO = baseService.findCheckInfo(UserUtils.getUserId(), reqDTO.getId());
        // 密码隐藏
        respDTO.setPassword("");
        return super.success(respDTO);
    }

    /**
     * 发送考试安排通知
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "发送考试安排通知")
    @PostMapping("/prepare-notify")
    public ApiRest create(@RequestBody ExamNotifyReqDTO reqDTO) {
        baseService.prepareNotify(reqDTO);
        return super.success();
    }

    @ApiOperation(value = "开放生成考试")
    @PostMapping("/openAISaveExam")
    public ApiRest openAISaveExam(@RequestBody OpenAIExamDTO openAIExamDTO){
        String examId=baseService.openAISaveExam(openAIExamDTO);
        return super.success(examId);
    }


    @ApiOperation(value = "开放生成考试2.0")
    @PostMapping(value = "/createPaper")
    public ApiRest createPaper(@RequestBody CreateAiExamDto createAiExamDto){
        openCreateAiExamService.createPaper(createAiExamDto);
        return super.success();
    }
}
